package com.capacitydorm.server.service;

import com.capacitydorm.server.common.vo.DataGridBean;
import com.capacitydorm.server.common.vo.VisitLogVo;
import com.capacitydorm.server.dao.StudentMapper;
import com.capacitydorm.server.dao.VisitLogMapper;
import com.capacitydorm.server.domain.Student;
import com.capacitydorm.server.domain.Visitlog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class VisitLogService {

    @Resource
    private VisitLogMapper visitLogMapper;

    @Resource
    private StudentMapper studentMapper;

    public DataGridBean findVisitVos(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<VisitLogVo> visitLogVos = new ArrayList<>();
        List<Visitlog> visitlogs = visitLogMapper.selectAllLog();
        for (Visitlog v : visitlogs) {
            VisitLogVo visitLogVo = new VisitLogVo(v.getId(),v.getUserId(),v.getKey(),v.getDate(),v.getState());
            Student byId = studentMapper.findById(v.getUserId().toString());
            visitLogVo.setUsername(byId.getName());
            visitLogVos.add(visitLogVo);
        }
        PageInfo<VisitLogVo> info = new PageInfo<>(visitLogVos);
        return new DataGridBean(info.getTotal(), info.getList());
    }

}
